package v1

import (
	"biyesheji/model"
	"biyesheji/utils/errmsg"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 控制器层
// 定义的code将在同名包下共享
var code int

// 查询所症状有信息
func GetAllSympthom(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}
	data, total := model.GetAllSympthom(pageSize, pageNum)
	code = errmsg.SUCCESS
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询不同部位的症状关键字
func GetKeyword(c *gin.Context) {
	part := c.Query("part")
	data, code := model.GetKeyword(part)
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询症状名称
func GetSympthomName(c *gin.Context) {
	part := c.Query("part")
	keyword := c.Query("keyword")
	data, code := model.GetSympthomName(part, keyword)
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 进一步查询关键字
func GetMoreKeyword(c *gin.Context) {
	part := c.Query("part")
	keyword := c.Query("keyword")
	data, code := model.GetMoreKeyword(part, keyword)
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询症状相关信息
func GetSomeInfo(c *gin.Context) {
	sympthomname := c.Query("sympthomname")
	data, code := model.GetSomeInfo(sympthomname)
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 修改症状
func EditSympthom(c *gin.Context) {
	var data model.Sympthom
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.EditSympthom(id, &data)
	c.JSON(200, gin.H{
		"status":  code,
		"message": errmsg.GetErrMsg(code),
	})
}

// 模糊查询单条信息
func GetLikeSympthom(c *gin.Context) {
	spinfo := c.Query("spinfo")
	data, code := model.GetLikeSympthom(spinfo)
	c.JSON(200, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 新增症状信息
func AddSympthom(c *gin.Context) {
	var data model.Sympthom
	_ = c.ShouldBindJSON(&data)
	code = model.AddSympthom(&data)
	c.JSON(200, gin.H{
		"status":  code,
		"message": errmsg.GetErrMsg(code),
	})
}
